package cn.zbmao.web.controller;

import cn.zbmao.web.bean.Constants;
import cn.zbmao.web.bean.User;
import cn.zbmao.web.services.QualificationServices;
import cn.zbmao.web.services.UserServices;
import cn.zbmao.web.util.DateUtil;
import cn.zbmao.web.util.HighlightToText;
import cn.zbmao.web.util.StringOperateUtil;
import cn.zbmao.web.util.dao.DaoFactory;
import cn.zbmao.web.util.dao.DaoUtil;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


public class NewsController extends Controller {
	
	
	public void index(){
        _new();
	}
	public void _new(){
		User user = (User)getSessionAttr("user");

        //资质
             List<String> quallist = DaoFactory.getUserDao().findUserQualificationNameAllStr(user.getInt("id"));




			Integer pags = getParaToInt(0, 1);
			int time_last_login = DateUtil.getDateToInt(user.getTimestamp("user_lastlogin_time"));
			Map<String, List<Record>> map = UserServices.getUserInfo(user.getInt("id"));
//              根据用户关注的信息，查询出最新信息
			Page<Record> pages =  DaoFactory.getNewsDao().findNewsPage(pags, 50,map , DaoUtil.unStaleDated);
            StringBuffer sbf=new StringBuffer();
            if(map.get("user_keyword").size()!=0){
                for(Record str:map.get("user_keyword")){
                    sbf.append(str.getStr("keyword")+" ");
                }
            }
            StringBuffer qualicationList= new StringBuffer();
            if(map.get("user_qualification").size()!=0){
                for(Record str:map.get("user_qualification")){
                    qualicationList.append(str.getInt("qualification_id")+" ");
                }
            }
			List<Record>newsList = pages.getList();
            setAttr("keyList",sbf.toString().trim());
            setAttr("qualicationList",qualicationList.toString().trim());
			for (Record record : newsList) {
//                给关键字添加高亮
				if (map.get("user_keyword").size()==0) {
					record.set("title_key", record.get("title"));
				}else{
					record.set("title_key", HighlightToText.highlight(StringOperateUtil.exChange(record.getStr("title")), map.get("user_keyword")));
				}
				if (record.getInt("stime") == 0) {
					record.set("startTime", "");
				}
				if (record.getInt("etime") == 0) {
					record.set("endTime", "");
				}
				record.set("hostName", Constants.getHostMap().get(record.getInt("host_id")));
//给资质添加高亮
                record.set("qualification", QualificationServices.getQualificationForShow(record.getStr("qualification"),quallist));
			}
			
			setAttr("time_last_login", time_last_login);
			setAttr("page", 1);
			setAttr("newsPage",pages);
			setAttr("newspath", "_new");


            //推出标题
            setAttr("title","资讯信息");


            render("/views/news/index.html");


		
	}
	public void _old(){
		User user = (User)getSessionAttr("user");
		Integer pags = getParaToInt(0, 1);
		int time_last_login = DateUtil.getDateToInt(user.getTimestamp("user_lastlogin_time"));
		Map<String, List<Record>> map = UserServices.getUserInfo(user.getInt("id"));
		Page<Record> pages =null;
        StringBuffer sbf=new StringBuffer();
        if(map.get("user_keyword").size()!=0){
            for(Record str:map.get("user_keyword")){
                sbf.append(str.getStr("keyword")+" ");
            }
        }

        StringBuffer qualicationList= new StringBuffer();
        if(map.get("user_qualification").size()!=0){
            for(Record str:map.get("user_qualification")){
                qualicationList.append(str.getInt("qualification_id")+" ");
            }
        }

        setAttr("keyList",sbf.toString().trim());
        setAttr("qualicationList",qualicationList.toString().trim());

		pages=  DaoFactory.getNewsDao().findNewsPage(pags, 50,map ,DaoUtil.staleDated);
        //关注资质
        //资质
        List<String> quallist = DaoFactory.getUserDao().findUserQualificationNameAllStr(user.getInt("id"));



		List<Record>newsList = pages.getList();
		for (Record record : newsList) {
			if (map.get("user_keyword").size()==0) {
				record.set("title_key", record.get("title"));
			}else{
				record.set("title_key", HighlightToText.highlight(StringOperateUtil.exChange(record.getStr("title")),map.get("user_keyword")));
			}
			if (record.getInt("stime") == 0) {
				record.set("startTime", "");
			}
			if (record.getInt("etime") == 0) {
				record.set("endTime", "");
			}
			record.set("hostName", Constants.getHostMap().get(record.getInt("host_id")));
            record.set("qualification", QualificationServices.getQualificationForShow(record.getStr("qualification"),quallist));
		}
		
		
		setAttr("time_last_login", time_last_login);
		setAttr("page", 0);
		setAttr("newsPage", pages);
		


		setAttr("newspath", "_old");
        //推出标题
        setAttr("title","资讯信息");


        render("/views/news/index.html");

		
		
	}

    /**
     * 匹配条件
     */
    public void condition(){
        User user = (User) getSessionAttr("user");

        //地区设置
        List<Record> list = DaoFactory.getUserDao().getUser_district(user.getInt("id"));

        List<Record>url_list1 = DaoFactory.getUserDao().getUser_district_un(user.getInt("id"));
        List<Record>url_list=DaoFactory.getUserDao().getUser_district_u(user.getInt("id"));
            for(Record record:url_list1){
                url_list.add(record);
            }


        for (Record record:list){
            if(record.getInt("loc_id")== Constants.LOC_ID_CITY){
                Record r=DaoFactory.getUserDao().getPidById(record.getInt("district_id"));

                record.set("name_d",r.getStr("name")+"  "+record.getStr("name"));
            }else{
                record.set("name_d",record.getStr("name"));
            }

        }

        setAttr("user_distruct_list", list);
        setAttr("district_list", url_list);

        //关键字设置
        List<Record>keyword_list = DaoFactory.getUserDao().getUser_key(user.getInt("id"));
        setAttr("keyword_list", keyword_list);

        //资质设置
        List<Record> qualificationList= DaoFactory.getUserDao().findUserQualificationNameAll(user.getInt("id"));
        List<Record> unQualificationList= DaoFactory.getUserDao().UnFindUserQualification(user.getInt("id"));
        setAttr("qualificationList", qualificationList);
        setAttr("unQualificationList", unQualificationList);
        setAttr("page", 2);
        setAttr("title","资讯信息");
        render("/views/news/condition.html");
    }

    /**
     * 招标详情也页面
     */
	public void detailsPage(){
        //推出标题
        Integer id = getParaToInt("id",0);
//        用户输入的关键字
//        String search_key=getPara("search_key",null);
        String keyList=getPara("keyList","");
        String qualification=getPara("qualification", "");
        if(qualification.equals("0")){
            qualification="";
        }
//        setAttr("sk",sk);
//        Record r=DaoFactory.getQualificationDao().findByIdWithIsOk(qualification);
        List<String>qualifications= Arrays.asList(qualification.split(" "));

        setAttr("keyList", keyList);
        setAttr("qualification", qualification);
		Record record = DaoFactory.getNewsDao().findNews(id);
		if (record.getInt("stime") == 0) {
			record.set("startTime", "");
		}
		if (record.getInt("etime") == 0) {
			record.set("endTime", "");
		}
        User user = (User) getSessionAttr("user");
//        Map<String, List<Record>> map = UserServices.getUserInfo(user.getInt("id"));
        //关注资质
        //资质
        List<String> quallist =new ArrayList<String>();
        if(qualifications!=null ){
         for(String str:qualifications){
             if(str!="" ||str=="0") {
//                 quallist.add(QualificationServices.getQualificationMap().get(Integer.parseInt(str)).getName());
                 quallist=Arrays.asList(QualificationServices.getQualificationMap().get(Integer.parseInt(str)).getSeachTerm());
             }
         }
        }

//     List<String> quallist = DaoFactory.getUserDao().findUserQualificationNameAllStr(user.getInt("id"));


        if(qualification!=null && quallist.size()>0){
            record.set("qualification",QualificationServices.getQualificationForShow(record.get("qualification").toString(), quallist));
        }else{
            record.set("qualification","");
        }
//
//        record.set("qualification", QualificationServices.getQualificationForShow(record.getStr("qualification"),quallist));

        setAttr("title", "招标信息-" + record.getStr("title"));
		setAttr("news",record );
		render("/views/news/detailsPage.html");
	}

	//中标公告详细信息
	public void result_detailsPage(){
        Integer id = getParaToInt("id",0);
        String keyList=getPara("keyList",null);
        setAttr("keyList", keyList);
//       通过id查询该条中标信息
		Record result_record = DaoFactory.getNewsDao().find_result_news(id);

		if (result_record.getInt("successful_time") == 0) {
			result_record.set("time", "");
		}


        setAttr("title","招标信息-"+result_record.getStr("title"));

		setAttr("news",result_record );
		render("/views/news/result_detailsPage.html");
	}
	
//	招标快照
	public void snapShot(){
        Integer id = getParaToInt();
//        获取关键字
        String keyList=getPara("keyList",null);
//        获取资质
        String qualification=getPara("qualification", "");
        List<String>qualifications= Arrays.asList(qualification.split(" "));
//        Integer sk=getParaToInt("sk",0);
        List<String> sea=null;
        if(keyList!=null) {
//            sea = StringOperateUtil.reString(keyList).trim().split(" ");
            sea=StringOperateUtil.getKeyword(keyList);
        }
//
        List<String> quallist =new ArrayList<String>();
        if(qualifications!=null ){
            for(String str:qualifications){
                if(str!="") {
                    quallist=Arrays.asList(QualificationServices.getQualificationMap().get(Integer.parseInt(str)).getSeachTerm());
                }
            }
        }

		Record record  = DaoFactory.getNewsDao().querySnapShot(id);
        Record record1=DaoFactory.getNewsDao().queryUrl(record.getInt("id"));
        User user = (User) getSessionAttr("user");
        Map<String, List<Record>> map = UserServices.getUserInfo(user.getInt("id"));
         String str="news/snapShot/"+id+"?keyList="+keyList+"&qualification="+qualification;
// 为关键字与资质添加高亮
		if (record!=null || sea!=null) {
                    if(sea!=null) {

                        record.set("html_key", HighlightToText.highlightSnapQuali(Jsoup.clean(StringOperateUtil.exChange(record.getStr("content")), Whitelist.basic()), sea,"keyword"));
                        List<String> list_key = HighlightToText.findKey(Jsoup.clean(StringOperateUtil.exChange(record.getStr("content")), Whitelist.basic()), sea, str);
                        setAttr("list_key", list_key);
                        setAttr("user_keywordLength", list_key.size());
                    }else{
                        record.set("html_key",record.getStr("content"));
                        setAttr("user_keywordLength",0);
                    }

                    if(quallist.size()>0){
                        record.set("html_key", HighlightToText.highlightSnapQuali(record.getStr("html_key"), quallist,"quali"));
                        List<String> list_Quali = HighlightToText.findQuali(record.getStr("html_key"), quallist, str);
                        setAttr("list_Quali", list_Quali);
                        setAttr("user_QualiLength", list_Quali.size());
                    }else{
                        record.set("html_key",record.getStr("html_key"));
                        setAttr("user_QualiLength",0);

                    }

                setAttr("title", "招标猫快照");
                setAttr("id", id);
                setAttr("record1", record1);
                setAttr("snapShot", record);
                setAttr("path", "news/snapShot");
                render("/views/news/snapShot.html");
//            }
            } else {
                render("/home/404.html");

        }
	}
//    中标快照
	public void result_snapShot(){
        Integer id = getParaToInt();
//       关键字
        String keyList=getPara("keyList",null);
        List<String> sea=null;
        if(keyList!=null) {
//            sea = StringOperateUtil.reString(keyList).trim().split(" ");
            sea=StringOperateUtil.getKeyword(keyList);
        }
		Record record  = DaoFactory.getNewsDao().result_snapShot(id);
//        锚点
        String str="news/result_snapShot/"+id+"?keyList="+keyList;
        User user = (User) getSessionAttr("user");
        Map<String, List<Record>> map = UserServices.getUserInfo(user.getInt("id"));
//        添加关键字高亮
        if (record!=null || sea!=null) {
            if(sea!=null) {
                record.set("html_key", HighlightToText.highlightSnapQuali(Jsoup.clean(StringOperateUtil.exChange(record.getStr("content")), Whitelist.basic()), sea,"keyword"));
                List<String> list_key = HighlightToText.findKey(Jsoup.clean(StringOperateUtil.exChange(record.getStr("content")), Whitelist.basic()), sea, str);
                setAttr("list_key", list_key);
                setAttr("user_keywordLength", list_key.size());
            }else{
                record.set("html_key",record.getStr("content"));
                setAttr("user_keywordLength",0);
            }
            setAttr("user_QualiLength",0);
            setAttr("title","招标猫快照");
            Record record1=DaoFactory.getNewsDao().queryUrl_z(id);
            setAttr("user_keyword",map.get("user_keyword"));
            setAttr("id",id);
            setAttr("record1",record1);
			setAttr("snapShot", record);
            setAttr("path","news/result_snapShot");
			render("/views/news/snapShot.html");
		}else{
			render("/home/404.html");
		}	
	}
}
